package com.chyb.cloud.controller;

import com.chyb.cloud.entities.Pay;
import com.chyb.cloud.entities.PayDTO;
import com.chyb.cloud.resp.ResultData;
import com.chyb.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pay")
@Tag(name = "支付模块", description = "支付模块的CRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResultData<String> add(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        return ResultData.success("新增，返回值：" + payService.add(pay));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    public ResultData<String> delete(@PathVariable("id") Integer id) {
        return ResultData.success("删除，返回值：" + payService.delete(id));
    }

    @PutMapping("/update")
    @Operation(summary = "修改")
    public ResultData<String> update(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        return ResultData.success("修改，返回值：" + payService.update(pay));
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "更具id进行查看")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        if (id < 1) throw new RuntimeException("id 不可以负数");
        return ResultData.success(payService.selectById(id));
    }

    @GetMapping("/getAll")
    @Operation(summary = "查询全部")
    public ResultData<List<Pay>> getAll() {
        return ResultData.success(payService.getAll());
    }

    @Value("${server.port}")
    private String port;

    @GetMapping("/getInfo")
    @Operation(summary = "查询配置")
    public String getInfo(@Value("${chyb.configInfo}") String chybInfo) {
        return "chyb configInfo:" + chybInfo + ",port:" + port;
    }
}
